function graphsVec=plotPages(varMat,inStru,nameStru)
% =========================================================================
%% plotDecompPerPage(varMat,obsnames,shonames,title,offset);
%
% 1) *varMat*      [nobs NC NP] matrix which decomposes each column of
%                  pivot Mat across pages 
%
% 3) *inStru*      Structure with all inputs for the graph, 
%                  including 
% Required
%
%  .'orgBy'         Organize by 'cols' or 'pages'
%  .'legend'        Of the two lines per plot 
%
% Optional 
%  .'xaxis'        Vector of X-axis 
%  .'suptitle'     String to add to the dimension being ploted in titles 
% *nameStru*  .colNames 
%                .pageNames
%
%
% Alejandro Justiniano January 21st 2010
% =========================================================================
[NR NC NP]=size(varMat); 
%% 3. Defaults
[~,inStru]=ch_field(inStru,'xOffset',0.1); 
[~,inStru]=ch_field(inStru,'yOffset',0.01); 
[~,inStru]=ch_field(inStru,'flagBoxOff',1); 
[~,inStru]=ch_field(inStru,'colors',[0 0 1;0 0 0]); 
[~,inStru]=ch_field(inStru,'lineWidths',1.5); 
[~,inStru]=ch_field(inStru,'NFigRows',4); 
[~,inStru]=ch_field(inStru,'NFigCols',2); 
[~,inStru]=ch_field(inStru,'suptitle',[]); 
[~,inStru]=ch_field(inStru,'suptitleFlush','left'); 
[~,inStru]=ch_field(inStru,'xaxis',(1:NR)'); 
[~,inStru]=ch_field(inStru,'flagNoLegend',1); 
[~,inStru]=ch_field(inStru,'paperPosition',[0.75 0.5 7.5 10]);
[~,inStru]=ch_field(inStru,'hlineOn',1);
[~,inStru]=ch_field(inStru,'hlineVal',0);
[~,inStru]=ch_field(inStru,'hlineStyle','--');

inStru=plotAssignDefaults(inStru);
%% 2. Check sizes
if length(nameStru.colNames)~=NC; 
    error('colNames does not match number of columns in CMAT'); 
end 
if length(nameStru.pageNames)~=NP; 
    error('pageNames does not match number of pages in CMAT'); 
end 
if isfield(inStru,'legend')==false; 
    error('Need provide inStru.legend'); 
end 
if isfield(inStru,'orgBy')==false; 
    disp('inStru.orgBy must be cols or pages'); 
    error('Need provide inStru.orgBy'); 
end 
%% Organize Plots by Columns or Pages? 
if strcmpi(inStru.orgBy,'COLS'); 
    flagOrgByCol=1; 
    NOuter=NC; 
    NInner=NP; 
elseif strcmpi(inStru.orgBy,'PAGES'); 
    flagOrgByCol=0; 
    NOuter=NP; 
    NInner=NC; 
else 
    error('Need define inStru.orgBy'); 
end 
if ~isempty(inStru.axesFontSize); 
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end 

%% Graph Settings 
NRows=inStru.NFigRows;
NCols=inStru.NFigCols;
graphsVec =zeros(ceil(NInner/(NRows*NCols)),NOuter);

for jj=1:NOuter
    ISubplots=0;
    IGraphs  =0;
    ICounter =0;
    %% Begin Inner Loop 
    for ii=1:NInner
        if ISubplots==0
            IGraphs=IGraphs+1;
            graphsVec(IGraphs,jj)=figure;
        end
        ISubplots=ISubplots+1;
        ICounter =ICounter+1;
        subplot(NRows,NCols,ISubplots);
        %         if ISubplots==NRows*NCols || ii==NInner
        %             inStru.flagNoLegend=0;
        %         else
        %             inStru.flagNoLegend=1;
        %         end
         if flagOrgByCol==true
            tempMat=squeeze(varMat(:,jj,ii));
            tempTit=nameStru.pageNames{ii};             
            tempSuptit=char(nameStru.colNames{jj}); 
        else
            tempMat=squeeze(varMat(:,ii,jj));
            tempTit=nameStru.colNames{ii};
            tempSuptit=char(nameStru.pageNames{jj});
        end 
        if isempty(inStru.suptitle)==false;
            if strcmpi(inStru.suptitleFlush,'LEFT')==1;
                tempSuptit=[char(inStru.suptitle),char(tempSuptit)];
            else
                tempSuptit=[char(tempSuptit),char(inStru.suptitle)];
            end
        end
        [~,~,~]=plot3sampSub(inStru.xaxis,tempMat,tempTit,1,inStru);
        if ii==NInner || ISubplots==NRows*NCols;  
            ISubplots=0;
            if size(graphsVec,1)==1
                supHand=suptitle(tempSuptit);
            else
                supHand=suptitle([tempSuptit,'   (',num2str(IGraphs),' of ',...
                    num2str(size(graphsVec,1)),')']);
            end
            set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
                'FontWeight','Bold');
            set(graphsVec(IGraphs,jj),'PaperPosition',inStru.paperPosition)
        end
    end
    if ~isempty(inStru.axesFontSize);
        set(0,'DefaultAxesFontSize','remove');
    end
end 